package com.cdu.guoxinan.www.smartcom.service.impl;

import com.cdu.guoxinan.www.smartcom.domian.pojo.HealthServiceAppointment;
import com.cdu.guoxinan.www.smartcom.mapper.HealthServiceAppointmentMapper;
import com.cdu.guoxinan.www.smartcom.service.HealthServiceAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HealthServiceAppointmentServiceImpl implements HealthServiceAppointmentService {
    @Autowired
    private HealthServiceAppointmentMapper appointmentMapper;

    @Override
    public void addAppointment(HealthServiceAppointment appointment) {
        appointmentMapper.insert(appointment);
    }

    @Override
    public List<HealthServiceAppointment> getAppointments(int page, int limit) {
        int offset = (page - 1) * limit;
        return appointmentMapper.selectPage(offset, limit);
    }

    @Override
    public int getTotalCount() {
        return appointmentMapper.countAll();
    }

    @Override
    public List<HealthServiceAppointment> getAppointmentsByResidentId(Long residentId, Integer serviceType, Integer status, String appointmentDate, int page, int limit) {
        int offset = (page - 1) * limit;
        return appointmentMapper.selectPageByResidentId(residentId, serviceType, status, appointmentDate, offset, limit);
    }

    @Override
    public int getTotalCountByResidentId(Long residentId, Integer serviceType, Integer status, String appointmentDate) {
        return appointmentMapper.countAllByResidentId(residentId, serviceType, status, appointmentDate);
    }

    // 新增：取消预约（更新状态）
    @Override
    public boolean cancelAppointment(Long appointmentId) {
        return appointmentMapper.updateStatus(appointmentId, 3) > 0;
    }

    // 新增：删除预约
    @Override
    public boolean deleteAppointment(Long appointmentId) {
        return appointmentMapper.deleteById(appointmentId) > 0;
    }

    // 新增：多条件分页查询所有预约
    @Override
    public List<HealthServiceAppointment> getAppointmentsByCondition(Integer serviceType, Integer status, String appointmentDate, int page, int limit) {
        int offset = (page - 1) * limit;
        return appointmentMapper.selectPageByCondition(serviceType, status, appointmentDate, offset, limit);
    }
    @Override
    public int getTotalCountByCondition(Integer serviceType, Integer status, String appointmentDate) {
        return appointmentMapper.countAllByCondition(serviceType, status, appointmentDate);
    }

    // 新增：完成预约（将状态设为2）
    @Override
    public boolean finishAppointment(Long appointmentId) {
        return appointmentMapper.updateStatus(appointmentId, 2) > 0;
    }
} 